import timLogin from '@/utils/timLogin.js' // 导入tim 登录
import formatMessageDate from '@/utils/commen/imTimeFormat.js'
import pinyin from './components/Convert_Pinyin.js'
import defaultImg from '@/components/defaultImg/defaultImg' //默认图
export default {
  components: {
    defaultImg
  },
  data() {
    return {
      pageLoading: false,
      url: '', //地址
      isShow: false,
      jumperIndex: 0, //滚动的位置
      jumperName: '', //点击的拼音
      value1: 1,
      topList: [],
      show: false,
      downStatus: false,
      anchor: 'anchor-A', // 列表锚点
      indexList: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
        'V', 'W', 'X', 'Y', 'Z', '#'
      ],
      keyword: '', // 搜索关键词
      popupShow: false,
      groupInfo: null,
      isLeftClick: false,
      height: 0,
      scHeight: 83, //滚动视图高度
      myFriend: [], //我的好友列表
      contactList: [], //截取到的分组列表
      shareList: {}, //点击分享列表
      pageSize: 99,
      imType: 'C2C',
      img: '',
      title: '',
      scrollTop: 0
    }
  },
  onLoad(op) {
    this.img = decodeURIComponent(op.img)
    this.title = op.title
    this.url = decodeURIComponent(op.url)
    console.log(op)

    // 登录tim
    if (this.$store.state.isSDKReady) {
      this.getMyGoodFriendList()
    } else {
      timLogin(() => {
        this.getMyGoodFriendList()
      })
    }
  },
  created() {

  },
  onPageScroll(e) {
    this.scrollTop = e.scrollTop
  },
  computed: {

  },
  onReady() {

  },
  methods: {
    getNodesInfo(options) {
      const query = uni.createSelectorQuery().in(this) //获得
      //获取多个节点方式
      query.selectAll('.index-anchor').boundingClientRect(data => {

        this.topList = data.map(item => {
          return Math.ceil(item.top)
        })
        this.detail(options)
      }).exec()
    },
    detail(options) {
      //options  为滚动信息。  options.detail.scrollTop  值为相对于scroll-view。
      let topPage = options.detail.scrollTop + this.topList[0]

      for (let i = 0; i < this.topList.length; i++) {
        let node1 = this.topList[i]
        let node2 = this.topList[i + 1]
        if (node2 && topPage >= node1 && topPage < node2) {
          this.jumperIndex = i
          break
        } else if (node2 && topPage === node2) {
          this.jumperIndex = i + 1
          break
        }
      }
    },
    scroll(options) {
      //options  为滚动信息。  options.detail.scrollTop  值为相对于scroll-view。
      if (!uni.getStorageSync('resolve')) {
        this.getNodesInfo(options)
      };
    },
    //搜索
    search(val) {
      if (val.trim()) {
        this.keyword = val
        // this.myFriend = []
        this.getMyGoodFriendList()
      }
    },
    change(e) {
      if (e === '') {
        this.getMyGoodFriendList()
      }
    },
    //分享
    share(item) {

      this.show = true
      this.shareList = item
    },
    jumper(event, i) {
      this.jumperIndex = i
      this.jumperName = event
      this.isLeftClick = true,

      this.jumperIndex = event
      let len = this.contactList[i].data.length
      if (event === '#') {
        this.anchor = 'anchor-YZ'
        return

      }
      // if (len > 0) {
      this.anchor = 'anchor-' + event
      // }

    },

    isChinese(temp) {
      var re = /[^\u4E00-\u9FA5]/
      if (re.test(temp)) {
        return false
      }
      return true
    },
    isChar(char) {
      var reg = /[A-Za-z]/
      if (!reg.test(char)) {
        return false
      }
      return true
    },
    pySort() {
      var $this = this
      if (!String.prototype.localeCompare) {
        return null
      }

      var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ#'.split('') //ABCDEFGHJKLMNOPQRSTWXYZ
      var zh = '阿八嚓哒妸发旮哈*讥咔垃痳拏噢妑七呥涩它**穵夕丫帀'.split('')
      var result = []
      let tr = ''
      var curr

      for (let i = 0; i < letters.length; i++) {
        curr = {
          letter: letters[i],
          data: []
        } //字母对象，data数据

        this.myFriend.forEach(n => {

          let initial = n.userName.charAt(0) //截取第一个字符

          if (initial === letters[i] || initial === letters[i].toLowerCase()) { //首字符是英文的
            curr.data.push(n)
          }
          if (zh[i] !== '*' && $this.isChinese(initial)) { //判断是否是无汉字,是否是中文

            let chinaName = pinyin.chineseToPinYin(initial).charAt(0) //直接使用pinyin中方法

            if (chinaName === letters[i] || chinaName === letters[i].toLowerCase()) { //首字符是英文的
              curr.data.push(n)

            }

          }
          if (letters[i] === '#' && !$this.isChar(initial) && !$this.isChinese(initial)) {
            curr.data.push(n)

          }
        })
        result.push(curr)

      }
      this.contactList = result //contactList 是data中定义的 []

    },

    // 获取我的好友
    async getMyGoodFriendList() {
      uni.showLoading({
        title: '加载中'
      })
      let data = {
        pageIndex: 1,
        pageSize: this.pageSize,
        isCostMoney: -1,
        accountType: -1,
        keyWords: this.keyword,
      }
      try {
        const res = await this.$api.chat.GetMyPartnerList(data)
        this.myFriend = res.data
        this.pageLoading = true
        this.pySort()
        uni.hideLoading()

      } catch (e) {
        uni.hideLoading()
      }
    },
    cancel() {
      this.show = false
    },
    //发送
    send() {

      this.show = false
      let info = {
        summary: '',
        imageUrl: this.img,
        title: this.title,
        href: this.url
      }
      let message = this.tim.createCustomMessage({
        to: this.shareList.uid,
        conversationType: 'C2C',
        payload: {
          data: 'share',
          description: '',
          extension: JSON.stringify(info)
        }
      })
      let pomise = this.tim.sendMessage(message)
      pomise.then(res => {

        uni.navigateTo({
          url: '/pages_chat/chat/chat?userid=' + this.shareList.uid + '&type=C2C'
        })
      }).catch(err => {

      })
    }
  }
}
